package com.csun.dsms.utils;

import jxl.Workbook;
import jxl.format.UnderlineStyle;
import jxl.format.VerticalAlignment;
import jxl.write.*;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

public class ExcelUtil {

    public static void downExcel(HttpServletResponse response,String filename, List<String> columnNames){



        try {
            response.setCharacterEncoding("UTF-8");
            response.setContentType("application/vnd.ms-excel");
            response.setHeader("Content-Disposition",
                    "attachment;filename=" + new String(filename.getBytes("UTF-8"),"ISO8859-1") + ".xls");


           /* WritableFont wft = new WritableFont(WritableFont.ARIAL,20,WritableFont.BOLD,false,UnderlineStyle.NO_UNDERLINE,Colour.BLACK);
            WritableCellFormat wcfmt = new WritableCellFormat(wft);
            wcfmt.setBorder(jxl.format.Border.ALL,jxl.format.BorderLineStyle.THIN);
            //设置水平居中
            wcfmt.setAlignment(Alignment.CENTRE);
            //设置垂直居中
            wcfmt.setVerticalAlignment(VerticalAlignment.CENTRE);
            wcfmt.setWrap(true);*/

            ServletOutputStream outputStream = response.getOutputStream();
            WritableWorkbook workbook = Workbook.createWorkbook(outputStream);
            WritableSheet sheet = workbook.createSheet(filename, 0);

            for (Integer i = 0; i < columnNames.size(); i++) {

                //设置列宽
                sheet.setColumnView(i, 25);
                //写入列名
                sheet.addCell(new Label(i,0,columnNames.get(i)));

            }



            workbook.write();
            workbook.close();
            outputStream.flush();
            outputStream.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    //第一种画笔
    public static WritableCellFormat wft(){

        WritableCellFormat wcfmt=null;
        try {

            WritableFont wft = new WritableFont(WritableFont.ARIAL,20,WritableFont.BOLD,false,UnderlineStyle.NO_UNDERLINE, jxl.format.Colour.BLACK);
             wcfmt = new WritableCellFormat(wft);
            wcfmt.setBorder(jxl.format.Border.ALL,jxl.format.BorderLineStyle.THIN);
            //设置水平居中
            wcfmt.setAlignment(jxl.format.Alignment.CENTRE);
            //设置垂直居中
            wcfmt.setVerticalAlignment(VerticalAlignment.CENTRE);
            wcfmt.setWrap(true);
        }catch (WriteException e) {
            e.printStackTrace();
        }

        return wcfmt;
    }

    //第二种画笔
    public static WritableCellFormat wft1(){
        WritableCellFormat wcfmt=null;
        try {

            WritableFont wft = new WritableFont(WritableFont.ARIAL,10,WritableFont.NO_BOLD,false,UnderlineStyle.NO_UNDERLINE, jxl.format.Colour.BLACK);
            wcfmt = new WritableCellFormat(wft);
            wcfmt.setBorder(jxl.format.Border.ALL,jxl.format.BorderLineStyle.THIN);

            wcfmt.setAlignment(jxl.format.Alignment.CENTRE);

            //设置垂直居中
            wcfmt.setVerticalAlignment(VerticalAlignment.CENTRE);
        }catch (WriteException e) {
            e.printStackTrace();
        }

        return wcfmt;
    }

    //第三种画笔
    public static WritableCellFormat wft2(){
        WritableCellFormat wcfmt=null;
        try {

            WritableFont wft = new WritableFont(WritableFont.ARIAL,12,WritableFont.BOLD,false,UnderlineStyle.NO_UNDERLINE, jxl.format.Colour.BLACK);
            wcfmt = new WritableCellFormat(wft);
            wcfmt.setBorder(jxl.format.Border.ALL,jxl.format.BorderLineStyle.THIN);
            //设置水平居中
            wcfmt.setAlignment(jxl.format.Alignment.CENTRE);
            //设置垂直居中
            wcfmt.setVerticalAlignment(VerticalAlignment.CENTRE);
            wcfmt.setWrap(true);
        }catch (WriteException e) {
            e.printStackTrace();
        }

        return wcfmt;
    }
}
